# -*- coding: utf-8 -*-
# (c) 2015 Mikel Arregi - AvanzOSC
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html

from odoo import fields, models, api


class StockMove(models.Model):
    _inherit = 'stock.move'

    def _get_stock_quant_resvere_ids(self):
        for move in self:
            line_obj = self.env['stock.quant']
            if move.location_id.usage == 'internal':
                res = line_obj.search(
                    [('product_id', '=', move.product_id.id), ('location_id', '=', move.location_id.id),
                     ('reservation_id', '=', move.id)]).ids
                move.quant_resvere_ids = res

    def _get_stock_quant_unresvere_ids(self):
        for move in self:
            line_obj = self.env['stock.quant']
            if move.location_id.usage == 'internal':
                res = line_obj.search(
                    [('product_id', '=', move.product_id.id), ('location_id', '=', move.location_id.id),
                     ('reservation_id', '=', False)]).ids
                move.quant_unresvere_ids = res

    quant_resvere_ids = fields.One2many('stock.quant', string=u'在库占用', compute=_get_stock_quant_resvere_ids)
    quant_unresvere_ids = fields.One2many('stock.quant', string=u'在库未占用', compute=_get_stock_quant_unresvere_ids)
    move_quant_for_pick_ids = fields.One2many('move.quant.for.pick', 'move_id', string=u'挑选增加')

    @api.multi
    def quant_create_for_pick(self):
        for_pick_obj = self.env['move.quant.for.pick']
        for move in self:
            for quant in move.quant_unresvere_ids:
                for_pick_obj.create({
                    'move_id': move.id,
                    'quant_id': quant.id,
                    'pick_quant_qty': quant.qty,
                })
        return self.split_move_lot()

    @api.multi
    def add_quant_for_move(self):
        lots_obj = self.env['stock.move.lots']
        for move in self:
            for pick in move.move_quant_for_pick_ids:
                has_same = False
                for lot in move.active_move_lot_ids:
                    if not has_same and lot.lot_id == pick.lot_id:
                        lot.quantity_done += pick.pick_quant_qty
                        has_same = True
                if not has_same:
                    lots_obj.create({
                        'move_id': move.id,
                        'lot_id': pick.lot_id.id,
                        'quantity_done': pick.pick_quant_qty,
                        # 因为有模块中写了这个判断可见的
                        'raw_material_production_id': move.raw_material_production_id.id,
                    })
                pick.unlink()
        return self.split_move_lot()


class stock_pack_operation(models.Model):
    _inherit = 'stock.pack.operation'

    def _get_stock_quant_resvere_ids(self):
        for pack in self:
            line_obj = self.env['stock.quant']
            if pack.location_id.usage == 'internal':
                moves = [links.move_id.id for links in pack.linked_move_operation_ids]
                res = line_obj.search(
                    [('product_id', '=', pack.product_id.id), ('location_id', '=', pack.location_id.id),
                     ('reservation_id', 'in', moves)]).ids
                pack.quant_resvere_ids = res

    def _get_stock_quant_unresvere_ids(self):
        for pack in self:
            line_obj = self.env['stock.quant']
            if pack.location_id.usage == 'internal':
                res = line_obj.search(
                    [('product_id', '=', pack.product_id.id), ('location_id', '=', pack.location_id.id),
                     ('reservation_id', '=', False)]).ids
                pack.quant_unresvere_ids = res

    quant_resvere_ids = fields.One2many('stock.quant', string=u'在库占用', compute=_get_stock_quant_resvere_ids)
    quant_unresvere_ids = fields.One2many('stock.quant', string=u'在库未占用', compute=_get_stock_quant_unresvere_ids)
    pack_quant_for_pick_ids = fields.One2many('pack.quant.for.pick', 'pack_id', string=u'挑选增加')

    @api.multi
    def quant_create_for_pick(self):
        for_pick_obj = self.env['pack.quant.for.pick']
        for pack in self:
            for quant in pack.quant_unresvere_ids:
                for_pick_obj.create({
                    'pack_id': pack.id,
                    'quant_id': quant.id,
                    'pick_quant_qty': quant.qty,
                })
        return self.action_split_lots()
        # res = self.action_split_lots()
        # res['target'] = 'current'
        # return res

    @api.multi
    def add_quant_for_pack(self):
        lots_obj = self.env['stock.pack.operation.lot']
        for pack in self:
            for pick in pack.pack_quant_for_pick_ids:
                has_same = False
                for lot in pack.pack_lot_ids:
                    if not has_same and lot.lot_id == pick.lot_id:
                        lot.qty += pick.pick_quant_qty
                        has_same = True
                if not has_same:
                    lots_obj.create({
                        'operation_id': pack.id,
                        'lot_id': pick.lot_id.id,
                        'qty': pick.pick_quant_qty,
                    })
                pick.unlink()
        return self.action_split_lots()
        # res = self.action_split_lots()
        # res['target'] = 'current'
        # return res
